<?php

namespace YmPhp\Admin\Http\Controllers\SiteCity;

use YmPhp\Admin\Models\SiteCity\SiteCity;
use YmPhp\Admin\Models\Member\MemberList;
use YmPhp\Admin\http\Repositories\SiteCityApply;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;

class SiteCityApplyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SiteCityApply(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('uid')->display(function ($value) {
                $member = (new MemberList())->getDetails($value);
                $memberInfo =  '用户昵称：'.$member->nickname.' 【UID:'.$member->uid.'】<br>';
                $memberInfo .=  '用户账号：'.$member->username.'<br>';
                return $memberInfo;
            })->width(200);
            $grid->column('checkstid','站点信息')->display(function ($value) {
                $site = (new SiteCity())->getCity($value);
                $siteInfo =  '申请站点城市：'.$site->checkstid.' 【ID:'.$site->checkstid.'】<br>';
                $siteInfo .=  '所属城市：'.$site->name2.' 【ID:'.$site->checkstid.'】<br>';
                $siteInfo .=  '分站名称：'.$site->name;
                return $siteInfo;

            })->width(200);
            $grid->column('note')->display(function ($value) {
                return '<code>'.$value.'</code>';
            })->width(200);
            $grid->column('price')->display(function ($value) {
                $html = '支付金额：<code>'.$value.'</code>';
                $html .= '<br>订单号：<span class="label label-success">'.$this->order_id.'</span>';
                if ($this->pay_at == '' || $this->pay_at == null)  {
                    $html .= '<br>支付时间：<span class="label label-danger">尚未支付</span>';
                } else {
                    $html .= '<br>支付时间：<span class="label label-success">'.$this->pay_at.'</span>';
                }
                return $html;
            })->width(200);
            $grid->column('updated_at')->sortable();
            $grid->disableViewButton();
            $grid->disableCreateButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SiteCityApply(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('stid');
            $show->field('order_id');
            $show->field('note');
            $show->field('price');
            $show->field('checkstid');
            $show->field('pay_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SiteCityApply(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('stid');
            $form->text('order_id');
            $form->text('note');
            $form->text('price');
            $form->text('checkstid');
            $form->text('pay_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
